package com.example.springboot_system.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot_system.mapper.OutpatientQueueMapper;
import com.example.springboot_system.mapper.PatientMapper;
import com.example.springboot_system.mapper.TollTakeDrugMapper;
import com.example.springboot_system.pojo.Patient;
import com.example.springboot_system.pojo.TollTakeDrug;
import com.example.springboot_system.pojo.User;
import com.example.springboot_system.service.PayService;
import com.example.springboot_system.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

@Service
public class PayImpl implements PayService {

    @Autowired(required = false)
    private PatientMapper patientMapper;

    @Autowired(required = false)
    private TollTakeDrugMapper tollTakeDrugMapper;

    @Override
    public ResponseResult MoneyPay(String cardId, String prescriptionNum, HttpSession session) {
        User user1= (User) session.getAttribute("user");
        if(user1==null){
            return new ResponseResult(201,"当前会话已超时，请重新登录");
        }

        //拿到卡号，判断是否为空，同时通过卡号拿到病人的ID
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("card_id", cardId);
        Patient patient = patientMapper.selectOne(queryWrapper);
        System.out.println("patient：" + patient);

        String id = UUID.randomUUID().toString();
        Date time = new Date();
        TollTakeDrug tollTakeDrug = new TollTakeDrug();
        tollTakeDrug.setId(id);
        tollTakeDrug.setCreateDatetime(time);
        tollTakeDrug.setPrescriptionNum(prescriptionNum);
        tollTakeDrug.setTakingDrugStatus(0);
        tollTakeDrug.setTollDateTime(time);
        tollTakeDrug.setTollOperator(user1.getId());
        tollTakeDrug.setPatientId(patient.getId());
        tollTakeDrugMapper.insert(tollTakeDrug);
        System.out.println(tollTakeDrug);
        return new ResponseResult(200,"提交成功");
    }

    @Override
    public ResponseResult CheckToPay(String prescriptionNum, HttpSession session) {
        QueryWrapper<TollTakeDrug> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("prescription_num", prescriptionNum);
        TollTakeDrug tollTakeDrug = tollTakeDrugMapper.selectOne(queryWrapper);
        if(tollTakeDrug != null && tollTakeDrug.getTakingDrugStatus()==0)
            return new ResponseResult(201,"该诊单已提交，请前往药房取药");
        else
            return new ResponseResult(200,"提交成功");
    }



}
